<?php

namespace App\Http\Controllers\API;

use App\Http\Resources\ExhibitorInformationResource;
use App\Models\ExhibitorInformation;
use App\Models\ExhibitorUser;
use App\Services\AliSmsService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class ExhibitorUserController extends Controller
{


    public function updatePWD(Request $request)
    {
        $exhibitor_information_id = session()->get('exhibitor_information_id');
        $exhibitor_user_orm = ExhibitorInformation::query()->find($exhibitor_information_id);
        $exhibitor_message_id = session()->get('exhibitor_message_id');
        $old_password = $request->get('old_password');

        if (!$old_password) {
            return $this->failure('请输入原密码');
        }

        $pwd_db = $exhibitor_user_orm->pwd;

        if ($pwd_db != $old_password) {
            return $this->failure('原密码错误');
        }
        if (!$exhibitor_information_id) {
            return $this->failure('登录超时，重新登录');
        }
        if (!$exhibitor_message_id) {

            return $this->failure('数据错误，重新登录');
        }

        $exhibitor_information_id = session('exhibitor_information_id');

        if (!$request->get('new_pwd')) {
            return $this->failure('请输入新密码');
        }
        if (!$request->get('confirm_pwd')) {
            return $this->failure('请再次确认密码');
        }

        $new_pwd = $request->get('new_pwd');
        $confirm_pwd = $request->get('confirm_pwd');

        if ($new_pwd != $confirm_pwd) {
            return $this->failure('两次密码不一致');
        }

        $pwd = $request->get('confirm_pwd');
        $pwd = Hash::make($pwd);

        $exhibitor_user = $exhibitor_user_orm->update(['password' => $pwd, 'pwd' => $new_pwd, 'is_update_pwd' => 1]);

        if ($exhibitor_user) {
            return $this->success('修改成功');
        } else {
            return $this->failure('失败');
        }
    }

}
